package controller

import (
	"context"
	"gomicro/httpMicro/httpClient/util"
	//"io/ioutil"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/selector"
	//"net/http"
	"github.com/micro/go-plugins/client/http/v2"
)

type StudentMicroController struct{
    Ctx  iris.Context
}

func (c *StudentMicroController) PostMicroinsert() mvc.Result {
	id := c.Ctx.FormValue("id")
	name := c.Ctx.FormValue("name")
	birth := c.Ctx.FormValue("birth")
	sex := c.Ctx.FormValue("sex")

	httpMicroSelector := selector.NewSelector(
		selector.Registry(util.EtcdRegistry),
		selector.SetStrategy(selector.RoundRobin),
	)

	NewClient := http.NewClient(
		client.Selector(httpMicroSelector),
		client.ContentType("application/json"),
	)

	req := NewClient.NewRequest("go.httpmicro.api.student.server", "/microinsert", map[string]string{"id" : id, "name" : name, "birth" : birth, "sex" : sex,})
	var res map[string]interface{}
	NewClient.Call(context.Background(), req, &res)
	return mvc.Response{
        Object : res,
    }
}

func (c *StudentMicroController) PostMicrodelete() mvc.Result {
	name := c.Ctx.FormValue("name")

	httpMicroSelector := selector.NewSelector(
		selector.Registry(util.EtcdRegistry),
		selector.SetStrategy(selector.RoundRobin),
	)

	NewClient := http.NewClient(
		client.Selector(httpMicroSelector),
		client.ContentType("application/json"),
	)

	req := NewClient.NewRequest("go.httpmicro.api.student.server", "/microdelete", map[string]string{"name" : name})
	var res map[string]interface{}
	NewClient.Call(context.Background(), req, &res)

	return mvc.Response{
		Object : res,
	}
}

func (c *StudentMicroController) PostMicroupdate() mvc.Result {
	id := c.Ctx.FormValue("id")
	name := c.Ctx.FormValue("name")
	birth := c.Ctx.FormValue("birth")
	sex := c.Ctx.FormValue("sex")

	httpMicroSelector := selector.NewSelector(
		selector.Registry(util.EtcdRegistry),
		selector.SetStrategy(selector.RoundRobin),
	)

	NewClient := http.NewClient(
		client.Selector(httpMicroSelector),
		client.ContentType("application/json"),
	)

	req := NewClient.NewRequest("go.httpmicro.api.student.server", "/microupdate", map[string]string{"id" : id, "name" : name, "birth" : birth, "sex" : sex,})
	var res map[string]interface{}
	NewClient.Call(context.Background(), req, &res)
	return mvc.Response{
        Object : res,
    }
}

func (c *StudentMicroController) PostMicroquery() mvc.Result {

	// getService , sErr := util.etcdRegistry.GetService("go.httpmicro.api.student.server")
	// if sErr != nil{
	// 	log.Fatal(sErr)
	// }
	// netx := selector.RoundRobin(getService)
	// note, err := netx()
	// if err != nil {
	// 	log.Fatal(note)
	// }

	// req, _ := http.NewRequest("POST", "http://"+ note.Address + "/query", nil)
	// client := http.DefaultClient
	// resb, _ := client.Do(req)
	// defer resb.Body.Close()
	// buf, _ := ioutil.ReadAll(resb.Body)
	// res := string(buf)


	httpMicroSelector := selector.NewSelector(
		selector.Registry(util.EtcdRegistry),
		selector.SetStrategy(selector.RoundRobin),
	)

	NewClient := http.NewClient(
		client.Selector(httpMicroSelector),
		client.ContentType("application/json"),
	)

	req := NewClient.NewRequest("go.httpmicro.api.student.server", "/microquery", map[string]interface{}{})
	var res interface{}
	NewClient.Call(context.Background(), req, &res)

	return mvc.Response{
		Object : res,
	}
}


